package com.ruoyi.web.controller.system;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Item;
import com.ruoyi.system.service.IItemService;
import com.ruoyi.system.service.impl.SysDeptServiceImpl;
import com.ruoyi.web.controller.tool.PageUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * 检查项目Controller
 *
 * @author ruoyi
 * @date 2023-07-04
 */
@Api(tags = "检查项目模块")
@RestController
@RequestMapping("/system/Item")
public class ItemController extends BaseController {
    @Autowired
    private IItemService itemService;

    private class Wrap implements Serializable {
        Item Item;
        String DeptId="";

        @JsonProperty("Item")
        public com.ruoyi.system.domain.Item getItem() {
            return Item;
        }

        @JsonProperty("DeptId")
        public String getDeptId() {
            return DeptId;
        }
    }

    @Autowired
    SysDeptServiceImpl sysDeptService;

    private Wrap convert(Item origin) {
        Wrap tempWrap = new Wrap();
        if(sysDeptService.selectDeptById(origin.getDeptId())!=null){
            tempWrap.DeptId=sysDeptService.selectDeptById(origin.getDeptId()).getDeptName();
        }
        tempWrap.Item = origin;
        return tempWrap;
    }

    /**
     * 查询检查项目列表
     */
    @ApiOperation(value = "查询检查项目列表")
    @PreAuthorize("@ss.hasPermi('system:Item:list')")
    @GetMapping("/list")
    public Page<Wrap> list(Item entity, Page<Item> page) {

        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        if (entity.getItemName() != null) {
            wrapper.like("ItemName", entity.getItemName());
        }
        if (entity.getMnemonicCode() != null) {
            wrapper.like("MnemonicCode", entity.getMnemonicCode());
        }
        if (entity.getItemCode() != null) {
            wrapper.like("IremCode", entity.getItemCode());
        }
        Page<Item> old = itemService.page(page, wrapper);
        Page<Wrap> pageWrap = new Page<>();
        List<Wrap> wraps = new ArrayList<>();
        List<Item> object = old.getRecords();
        for (Item item : object) {
            wraps.add(convert(item));
        }
        pageWrap.setRecords(wraps);
        PageUtil pageUtil = new PageUtil();
        pageUtil.setPage(pageWrap, old);
        return pageWrap;

    }

    /**
     * 导出检查项目列表
     */
    @ApiOperation(value = "导出检查项目列表")
    @PreAuthorize("@ss.hasPermi('system:Item:export')")
    @Log(title = "检查项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Item item) {
        LambdaQueryWrapper<Item> queryWrapper = new LambdaQueryWrapper<>();
        List<Item> list = itemService.list(queryWrapper);
        ExcelUtil<Item> util = new ExcelUtil<Item>(Item.class);
        util.exportExcel(response, list, "检查项目数据");
    }


    /**
     * 获取检查项目详细信息
     */
    @ApiOperation(value = "获取检查项目详细信息")
    @PreAuthorize("@ss.hasPermi('system:Item:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(itemService.getById(id));
    }

    /**
     * 新增检查项目
     */
    @ApiOperation(value = "新增检查项目")
    @PreAuthorize("@ss.hasPermi('system:Item:add')")
    @Log(title = "检查项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Item item) {
        return toAjax(itemService.save(item));
    }

    /**
     * 修改检查项目
     */
    @ApiOperation(value = "修改检查项目")
    @PreAuthorize("@ss.hasPermi('system:Item:edit')")
    @Log(title = "检查项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Item item) {
        return toAjax(itemService.updateById(item));
    }


    /**
     * 删除检查项目
     */
    @ApiOperation(value = "删除检查项目")
    @PreAuthorize("@ss.hasPermi('system:Item:remove')")
    @Log(title = "检查项目", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids) {
        return toAjax(itemService.removeBatchByIds(Arrays.asList(Ids)));
    }
}
